package cn.iocoder.an.module.business.service.storeinitbackup;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.an.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.an.module.system.service.permission.PermissionService;
import cn.iocoder.an.module.system.service.user.AdminUserService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.business.controller.admin.storeinitbackup.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.storeinitbackup.StoreInitBackupDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.storeinitbackup.StoreInitBackupMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 库存台账备份 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class StoreInitBackupServiceImpl implements StoreInitBackupService {

    @Resource
    private StoreInitBackupMapper storeInitBackupMapper;

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private PermissionService permissionService;

    @Override
    public Long createStoreInitBackup(StoreInitBackupSaveReqVO createReqVO) {
        // 插入
        StoreInitBackupDO storeInitBackup = BeanUtils.toBean(createReqVO, StoreInitBackupDO.class);
        storeInitBackupMapper.insert(storeInitBackup);

        // 返回
        return storeInitBackup.getId();
    }

    @Override
    public void updateStoreInitBackup(StoreInitBackupSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreInitBackupExists(updateReqVO.getId());
        // 更新
        StoreInitBackupDO updateObj = BeanUtils.toBean(updateReqVO, StoreInitBackupDO.class);
        storeInitBackupMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreInitBackup(Long id) {
        // 校验存在
        validateStoreInitBackupExists(id);
        // 删除
        storeInitBackupMapper.deleteById(id);
    }

    @Override
        public void deleteStoreInitBackupListByIds(List<Long> ids) {
        // 删除
        storeInitBackupMapper.deleteByIds(ids);
        }


    private void validateStoreInitBackupExists(Long id) {
        if (storeInitBackupMapper.selectById(id) == null) {
            throw exception(STORE_INIT_BACKUP_NOT_EXISTS);
        }
    }

    @Override
    public StoreInitBackupDO getStoreInitBackup(Long id) {
        return storeInitBackupMapper.selectById(id);
    }

    @Override
    public PageResult<StoreInitBackupDO> getStoreInitBackupPage(StoreInitBackupPageReqVO pageReqVO) {
        //查询自己的
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserDO user = adminUserService.getUser(loginUserId);
        if (user == null) {//管理员切换租户无法查询到
            pageReqVO.setCustomerAccount(null);
        } else {
            if (permissionService.hasAnyRoles(SecurityFrameworkUtils.getLoginUserId(), "super_admin", "tenant_admin")) {//判断是不是管理员
                pageReqVO.setCustomerAccount(null);
            }else{
                String userName = user.getUsername();
                pageReqVO.setCustomerAccount(userName);
            }

        }
        return storeInitBackupMapper.selectPage(pageReqVO);
    }

}